import {
	createRouter,
	createWebHashHistory
} from 'vue-router'
import Login from '../views/login.vue'
import Home from '../views/home.vue'
import Welcome from '../views/welcome.vue'
import Nf from '../views/noFound.vue'
import Search from '../views/search.vue'
import Quest from '../views/questionnaire.vue'
import Test from '../views/testing.vue'
import Classview from '../views/classview.vue'

const routes = [


	{
		path: '/',
		component: Login,
		meta: {
			title: "登录"
		},
	},
	{
		path: '/home',
		component: Home,
		meta: {
			title: "主页"
		},
		redirect: '/welcome',
		children: [{
				path: '/welcome',
				component: Welcome,
			},
			{
				path: '/search',
				component: Search
			},
			{
				path:'/question',
				component:Quest,
			},
			{
				path:'/testing',
				component:Test,
			},
			{
				path:'/class',
				component:Classview,
			},
			
		],
	},
	{
		path: "/404",
		component: Nf,
		meta: {
			title: "404"
		},
	},
	{
		path: "/:catchAll(.*)",
		redirect: "/404"
	},

]

const router = createRouter({
	history: createWebHashHistory(),
	routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
	if (to.meta.title) {
		document.title = to.meta.title;
	}
	if (to.path == '/') return next(); // 访问路径为登录
	// 获取flag
	const flagStr = window.sessionStorage.getItem("studentNo"); // session取值
	if (!flagStr) return next('/'); // 没登录去登录
	next();
})

export default router
